package com.gz.mylife.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gz.mylife.R;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 
 * @function 页面指示器
 * @author 陈希然
 *
 */
public class PageIndicator extends LinearLayout {
	
	private static final String TAG = PageIndicator.class.getSimpleName();
	
	private int mIndicatorSelected;
	private int mIndicatorUnselected;
	private int mIndicatorHeight;
	private int mIndicatorWidth;
	private int mIndicatorMargin;
	
	private LayoutParams mIndicatorParams;
	private OnPageChangeListener mDelegatePageListener;
	private PageListener mPageListener;
	
	private ImageView mCurrentSelected;
	private ViewPager mViewPager;

	public PageIndicator(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public PageIndicator(Context context, AttributeSet attrs) {
//		this(context, attrs, 0);
		super(context, attrs);
		init(context, attrs);
		// TODO Auto-generated constructor stub
	}

//	@SuppressLint("NewApi")
//	public PageIndicator(Context context, AttributeSet attrs, int defStyle) {
//		super(context, attrs, defStyle);
//		// TODO Auto-generated constructor stub
//
//		init(context, attrs);
//	}

	private void init(Context context, AttributeSet attrs) {
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		mIndicatorSelected = R.drawable.advert_indicator_selected;
		mIndicatorUnselected = R.drawable.advert_indicator_unselected;
		mIndicatorWidth = mIndicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 9, metrics);
		mIndicatorMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, metrics);
		
		TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.PageIndicator);
		try {
			mIndicatorWidth = (int) typeArray.getDimension(R.styleable.PageIndicator_indicatorWidth, mIndicatorWidth);
			mIndicatorHeight = (int) typeArray.getDimension(R.styleable.PageIndicator_indicatorHeight, mIndicatorHeight);
			mIndicatorMargin = (int) typeArray.getDimension(R.styleable.PageIndicator_indicatorMargin, mIndicatorMargin);
			mIndicatorSelected = typeArray.getResourceId(R.styleable.PageIndicator_indicatorSelected, mIndicatorSelected);
			mIndicatorUnselected = typeArray.getResourceId(R.styleable.PageIndicator_indicatorUnselected, mIndicatorUnselected);
			
		}finally {
			typeArray.recycle();
		}
		
		mIndicatorParams = new LayoutParams(mIndicatorWidth, mIndicatorHeight);
		mPageListener = new PageListener();
		setGravity(Gravity.CENTER_HORIZONTAL);
	}
	
	public void notifyDataSetChanged() {
		if(mViewPager.getAdapter() != null) {
			removeAllViews();
			int indicatorCount = mViewPager.getAdapter().getCount();
			for(int i = 0; i < indicatorCount; i++) {
//				int dot = i == 0 ? mIndicatorSelected : mIndicatorUnselected;
				ImageView indicator = new ImageView(getContext());
				indicator.setLayoutParams(mIndicatorParams);
				indicator.setImageResource(mIndicatorUnselected);
				LayoutParams params = (LayoutParams) indicator.getLayoutParams();
				params.leftMargin = params.rightMargin = mIndicatorMargin;
				addView(indicator);
			}
			
			if(indicatorCount > 0) {
//				mViewPager.getAdapter().
				mCurrentSelected = (ImageView) getChildAt(mViewPager.getCurrentItem());
				mCurrentSelected.setImageResource(mIndicatorSelected);
				CrashHandler.d(TAG, "current item " + mViewPager.getCurrentItem());
			}
		}
	}

	public void setOnPageChangeListener(OnPageChangeListener listener) {
		mDelegatePageListener = listener;
	}
	
	public void setViewPage(ViewPager viewPager) {		
		mViewPager = viewPager;
		mViewPager.setOnPageChangeListener(mPageListener);
		
	}
	
	private class PageListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			if (mDelegatePageListener != null) {
				mDelegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			if (mDelegatePageListener != null) {
				mDelegatePageListener.onPageScrollStateChanged(state);
			}
		}

		@Override
		public void onPageSelected(int position) {
			mCurrentSelected.setImageResource(mIndicatorUnselected);
			ImageView child = (ImageView) getChildAt(position);
			child.setImageResource(mIndicatorSelected);
			mCurrentSelected = child;
			if (mDelegatePageListener != null) {
				mDelegatePageListener.onPageSelected(position);
			}
		}

	}

}
